//-----------------------------------------------------------------------
// <copyright file="AddFile.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
// </copyright>
// <summary>Adds a file to an existing zip file.</summary>
//-----------------------------------------------------------------------
namespace Microsoft.Sdc.Tasks.Zip
{
    using System;
    using Microsoft.Build.Framework;

    /// <summary>
    /// Adds a file to an existing zip file using J# 2.0.
    /// </summary>
    /// <remarks>
    /// <code><![CDATA[<Zip.AddFile PathToZipFile="pathToZipFile" Path="path"/>]]></code>
    /// <para>where:</para>
    /// <para><i>pathToZipFile (Required)</i></para>
    /// <para>The path to the zip file to be added into.</para>
    /// <para><i>path (Required)</i></para>
    /// <para>The path to the file or folder (recursive) to be added.</para>
    /// </remarks>
    /// <example>
    /// <code><![CDATA[
    /// <Project>
    ///     <Target Name="Test" >
    ///         <Zip.AddFile
    ///             pathToZipFile="c:\test.zip"
    ///             pathToFile="C:\FileToAddToZipFile.txt" />
    ///     </Target>
    ///     <Target Name="Test" >
    ///         <Zip.AddFile
    ///             pathToZipFile="c:\test.zip"
    ///             pathToFile="C:\TestFiles" />
    ///     </Target>
    /// </Project>
    /// ]]></code>    
    /// </example>
    public class AddFile : TaskBase
    {
        private bool includeFolderPath = true;
        private string pathToFile = String.Empty;
        private string pathToZipFile = String.Empty;
        private string rootFolder = String.Empty;

        /// <summary>
        /// Specifies the path to the file, or folder (recursive) to be added to the specified zip
        /// </summary>
        /// <value>The full path to the file or folder.</value>
        [Required]
        public string PathToFile
        {
            get { return this.pathToFile ?? String.Empty; }
            set { this.pathToFile = value; }
        }

        /// <summary>
        /// Specifies the path to and filename of the zip file the add action is to be performed on.
        /// </summary>
        /// <value>The path to and filename of the zip file.</value>
        [Required]
        public string PathToZipFile
        {
            get { return this.pathToZipFile ?? String.Empty; }
            set { this.pathToZipFile = value; }
        }

        /// <summary>
        /// Specifies if the folders should be created 1:1 inside the ZIP file. If you specify false
        /// only the file names will be added to the ZIP archive
        /// </summary>
        public bool IncludeFolderPath
        {
            get { return this.includeFolderPath; }
            set { this.includeFolderPath = value; }
        }

        /// <summary>
        /// Specifies the root of the ZIP archive from the original location. All files contained in the root
        /// of this folder will be added flat into the ZIP file any subfolder will be added correctly to the root of the zip.
        /// <example>
        ///     rootFolder = C:\Dev\folderToZip (folder contains file 1.txt, 2.txt and sub folder 'subfolder' with the file 3.txt)
        ///     ZIP-Archive will contain: 
        ///     1.txt
        ///     2.txt
        ///     subfolder\3.txt
        /// </example>
        /// </summary>
        public string RootFolder
        {
            get { return this.rootFolder; }
            set { this.rootFolder = value; }
        }

        /// <summary>
        /// Performs the action of this task.
        /// </summary>
        protected override void InternalExecute()
        {
            this.Log.LogMessageFromResources("Zip.AddFileToZipFile", this.pathToFile, this.pathToZipFile);
            ZipManager.Add(this.pathToZipFile, this.pathToFile, this.includeFolderPath, this.rootFolder);
        }
    }
}